package com.skyline.wizard.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.springframework.jdbc.core.RowMapper;

import com.skyline.user.type.Sex;

public class UserDocMapper implements RowMapper<Document> {

	private final static String COLUMN_BIRTHDAY = "birthday";
	private final static String COLUMN_CITY = "city";
	private final static String COLUMN_COUNTRY = "country";
	private final static String COLUMN_EMAIL = "email";
	private final static String COLUMN_GRADUATE_SCHOOL = "graduateSchool";
	private final static String COLUMN_JOB = "job";
	// private final static String COLUMN_MOBILE_PHONE = "mobilephone";
	// private final static String COLUMN_MSN = "msn";
	private final static String COLUMN_NAME = "name";
	private final static String COLUMN_NICKNAME = "nickname";
	private final static String COLUMN_PORTRAIT = "portrait";
	private final static String COLUMN_PROVINCE = "province";
	// private final static String COLUMN_QQ = "qq";
	private final static String COLUMN_SEX = "sex";
	private final static String COLUMN_ID = "id";

	private static UserDocMapper mapper = new UserDocMapper();

	private static DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

	public static UserDocMapper getMapper() {
		return mapper;
	}

	@Override
	public Document mapRow(ResultSet rs, int rowNum) throws SQLException {
		Document d = new Document();

		// 增加birthday
		Date birthday = rs.getDate(COLUMN_BIRTHDAY);
		if (birthday == null) {
			birthday = new Date();
		}
		Field f = new Field(COLUMN_BIRTHDAY, formatter.format(birthday), Store.YES, Index.NOT_ANALYZED);
		d.add(f);

		// 增加city
		String city=rs.getString(COLUMN_CITY);
		if(city==null){
			city="";
		}
		f = new Field(COLUMN_CITY,city, Store.YES, Index.NOT_ANALYZED);
		d.add(f);

		// 增加country
		String country=rs.getString(COLUMN_COUNTRY);
		if(country==null){
			country="";
		}
		f = new Field(COLUMN_COUNTRY, country, Store.YES, Index.NOT_ANALYZED);
		d.add(f);

		// 增加email
		f = new Field(COLUMN_EMAIL, rs.getString(COLUMN_EMAIL), Store.YES, Index.ANALYZED);
		d.add(f);

		// 增加graduate school
		String graduateSchool=rs.getString(COLUMN_GRADUATE_SCHOOL);
		if(graduateSchool==null){
			graduateSchool="";
		}
		f = new Field(COLUMN_GRADUATE_SCHOOL, graduateSchool, Store.YES, Index.ANALYZED);
		d.add(f);

		// 增加job
		String job=rs.getString(COLUMN_JOB);
		if(job==null){
			job="";
		}
		f = new Field(COLUMN_JOB, job, Store.YES, Index.ANALYZED);
		d.add(f);

		// 增加name
		String name=rs.getString(COLUMN_NAME);
		if(name==null){
			name="";
		}
		f = new Field(COLUMN_NAME,name, Store.YES, Index.ANALYZED);
		d.add(f);

		// 增加nickname
		f = new Field(COLUMN_NICKNAME, rs.getString(COLUMN_NICKNAME), Store.YES, Index.ANALYZED);
		d.add(f);

		// 增加portrait
		f = new Field(COLUMN_PORTRAIT, rs.getString(COLUMN_PORTRAIT), Store.YES, Index.NOT_ANALYZED);
		d.add(f);

		// 增加province
		String province=rs.getString(COLUMN_PROVINCE);
		if(province==null){
			province="";
		}
		f = new Field(COLUMN_PROVINCE, province, Store.YES, Index.NOT_ANALYZED);
		d.add(f);

		// 增加sex
		String sex = rs.getString(COLUMN_SEX);
		if (sex == null) {
			sex = Sex.MALE.toString();
		}
		f = new Field(COLUMN_SEX, sex, Store.YES, Index.NOT_ANALYZED);
		d.add(f);

		// 增加id
		f = new Field(COLUMN_ID, rs.getString(COLUMN_ID), Store.YES, Index.NOT_ANALYZED);
		d.add(f);

		return d;
	}
}
